#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QProcess>
#include <QThread>
#include <QMessageBox>
#include <QThread>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow), solver(0)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_solve_clicked()
{
    if (solver && solver->isRunning())
    {
        QMessageBox::information(this, "Cancelled", "Another computation process is running");
        return ;
    }

    BaseGaussSeidelSolver * inner_solver = createSolver();
    solver = new SimpleSolver(inner_solver);
    inner_solver->setParent(solver);
    connect(solver, SIGNAL(finished()), this, SLOT(showResults()));
    connect(solver, SIGNAL(terminated()), this, SLOT(showResults()));

    solver->start();

//    if (ui->radioButton_Trapezium->isChecked())
//    {
//        RotatedGaussSeidelSolver * slv = dynamic_cast<RotatedGaussSeidelSolver*>(solver);
//        slv->turnCoordinatesBack();
//    }
//    solver->showGraph();
//    delete solver;
}

//void MainWindow::on_pushButton_solveNonrotated_clicked()
//{
////    GaussSeidelSolver solver(ui->nLineEdit->text().toInt(),
////                             ui->mu1LineEdit->text().toDouble(),
////                             ui->e1LineEdit->text().toDouble());
////    solver.setF1(ui->f1LineEdit->text().toDouble());
////    solver.setF2(ui->f2LineEdit->text().toDouble());
////    solver.setEps(ui->epsLineEdit->text().toDouble());
////    if (ui->checkBox_randomInit->isChecked())
////        solver.randomlyInitialize();
////    solver.start();
////    solver.showGraph();
//}

void MainWindow::on_checkBox_unpenetration_clicked(bool checked)
{
    if (checked)
        ui->checkBox_lagrangeFunction->setChecked(false);
}

void MainWindow::on_checkBox_friction_clicked(bool checked)
{
//    if (checked)
//        ui->checkBox_unpenetration->setChecked(false);
}

void MainWindow::on_pushButton_solve2_clicked()
{
//    RotatedGaussSeidelSolver2 solver(ui->nLineEdit->text().toInt(),
//                                     ui->mu1LineEdit->text().toDouble(),
//                                     ui->e1LineEdit->text().toDouble());
//    solver.setF1(ui->f1LineEdit->text().toDouble());
//    solver.setF2(ui->f2LineEdit->text().toDouble());
//    solver.setEps(ui->epsLineEdit->text().toDouble());
//    solver.setG_tr(ui->gTrLineEdit->text().toDouble());
//    if (ui->checkBox_randomInit->isChecked())
//        solver.randomlyInitialize();
//    if (ui->checkBox_unpenetration->isChecked())
//        solver.setUnpenetrationCondition(true);
//    if (ui->checkBox_friction->isChecked())
//        solver.setFrictionCondition(true);
//    solver.solve();
//    solver.turnCoordinatesBack();
//    solver.showGraph();
}

BaseGaussSeidelSolver * MainWindow::createSolver(QObject * parent)
{
    BaseGaussSeidelSolver * temp_solver = 0;
    if (ui->radioButton_Trapezium->isChecked())
    {
        temp_solver = new RotatedGaussSeidelSolver(ui->nLineEdit->text().toInt(),
                                                   ui->mu1LineEdit->text().toDouble(),
                                                   ui->e1LineEdit->text().toDouble(),
                                                   parent);
    }
    else if(ui->radioButton_Quad->isChecked())
    {
        temp_solver = new QuadGaussSeidelSolver(ui->nLineEdit->text().toInt(),
                                                ui->mu1LineEdit->text().toDouble(),
                                                ui->e1LineEdit->text().toDouble(),
                                                parent);
    }
    else if (ui->radioButton_TrapeziumNonRotated->isChecked())
    {
        temp_solver = new GaussSeidelSolver(ui->nLineEdit->text().toInt(),
                                            ui->mu1LineEdit->text().toDouble(),
                                            ui->e1LineEdit->text().toDouble(),
                                            parent);
    }
    temp_solver->setF1(ui->f1LineEdit->text().toDouble());
    temp_solver->setF2(ui->f2LineEdit->text().toDouble());
    temp_solver->setEps(ui->epsLineEdit->text().toDouble());
    temp_solver->setG_tr(ui->gTrLineEdit->text().toDouble());
    if (ui->checkBox_randomInit->isChecked())
        temp_solver->randomlyInitialize();
    if (ui->checkBox_unpenetration->isChecked())
        temp_solver->setUnpenetrationCondition(true);
    if (ui->checkBox_friction->isChecked())
        temp_solver->setFrictionCondition(true);
    if (ui->checkBox_lagrangeFunction->isChecked())
        temp_solver->setLagrangeFunctionCondition(true);

    return temp_solver;
}

void MainWindow::on_pushButton_solveUzawa_clicked()
{
    if (solver && solver->isRunning())
    {
        QMessageBox::information(this, "Cancelled", "Another computation process is running");
        return ;
    }

    BaseGaussSeidelSolver * inner_solver = createSolver();
    solver = new UzawaGaussSeidelSolver(inner_solver);
    inner_solver->setParent(solver);
    UzawaGaussSeidelSolver * temp = dynamic_cast<UzawaGaussSeidelSolver *> (solver);
    temp->setRo(ui->roLineEdit->text().toDouble());
    temp->setUzawaEps(ui->uzawaEpsLineEdit->text().toDouble());
    connect(solver, SIGNAL(finished()), this, SLOT(showResults()));
    connect(solver, SIGNAL(terminated()), this, SLOT(showResults()));

    solver->start();


    //connect(uzawa_solver::)
    //uzawa_solver->start();
//    UzawaGaussSeidelSolver solver(ui->nLineEdit->text().toInt(),
//                                  ui->mu1LineEdit->text().toDouble(),
//                                  ui->e1LineEdit->text().toDouble());
//    solver.setF1(ui->f1LineEdit->text().toDouble());
//    solver.setF2(ui->f2LineEdit->text().toDouble());
//    solver.setEps(ui->epsLineEdit->text().toDouble());
//    solver.setG_tr(ui->gTrLineEdit->text().toDouble());

//    if (ui->checkBox_randomInit->isChecked())
//        solver.randomlyInitialize();
//    if (ui->checkBox_unpenetration->isChecked())
//        solver.setUnpenetrationCondition(true);
//    if (ui->checkBox_friction->isChecked())
//        solver.setFrictionCondition(true);
//    solver.solve();
//    solver.turnCoordinatesBack();
//    solver.showGraph();
}

void MainWindow::on_checkBox_lagrangeFunction_clicked(bool checked)
{
    if (checked)
        ui->checkBox_unpenetration->setChecked(false);
}

void MainWindow::showResults()
{
    if (solver)
    {
        solver->showGraph();
        delete solver;
        solver = 0;
    }
    //isRunning = false;
    //ui->pushButton_solve->setText("Solve");
}

void MainWindow::on_pushButton_stop_clicked()
{
    if (/*uzawa_solver && uzawa_solver->isRunning() && */solver && solver->isRunning())
    {
        solver->stop();
        return ;
    }
}
